package com.example.mac.yuejian14.model;

import java.util.List;

import com.example.mac.yuejian14.listener.RetrofitListener1;
import com.example.mac.yuejian14.bean.ActivityBean;
import com.example.mac.yuejian14.service.ActivityService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/10/25.
 */

public class ActivityModel {

    private Retrofit retrofit;

    public ActivityModel() {
        //创建实例
        retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2/neu12_yuejian/index.php/Api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //活动列表
    public Call<List<ActivityBean>> getActivityList() {
        ActivityService activityService = retrofit.create(ActivityService.class);
        return activityService.getActivityList();
    }

    //获取活动详情
    public void getActivityById(String actid, final RetrofitListener1 listener) {
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.getActivityById(actid);
        Callback<ActivityBean> callback = new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                ActivityBean getTrainById = response.body();
                listener.onSuccess(getTrainById, 1);
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

    //搜索
    public Call getActivityBySearch(String actname) {
        ActivityService activityService = retrofit.create(ActivityService.class);
        return activityService.getActivityBySearch(actname);
    }

    //报名
    public Call addActivity(int uid, int actid) {
        ActivityService activityService = retrofit.create(ActivityService.class);
        return activityService.addActivity(uid, actid);
    }
}

